package org.example.leetcode;


import java.util.ArrayList;
import java.util.List;

/**
 * @author: lynn
 * @Descript:
 * @date: 2021/8/5 20:10
 * @version: 1.0
 */
public class LC45 {
    public static void main(String[] args) {
        int[] nums={2,3,1,1,4};
        LC45 lc=new LC45();
        List<Integer> list=new ArrayList<>();
        System.out.println(lc.jump(nums));
        System.out.println(lc.jump2(nums));
    }
    //fixme https://www.bilibili.com/video/BV1164y147os?from=search&seid=6744071307308793224
    public int jump(int[] nums) {
        if (nums==null||nums.length==0) return 0;
        int start=0;
        int end=nums.length-1;
        List<Integer> list=new ArrayList<>();
        helper(nums,start,end,list);
        return list.size();
    }
    //fixme

    /**
     *  核心解法：贪心，都是从小开始数，数到最大
     *  从后往前，  nums[i]+i>=end ,我就将这个end 替换为 i ，break；继续递归，start不变，变的是end
     */
    private void helper(int[] nums,int start ,int end,List<Integer> list){
        if (end==0) return;
        int index=0;
        for (int i = start; i <end ; i++) {
            if (nums[i]+i>=end){
                index=i;
                break;
            }
        }
        list.add(index);
        helper(nums,start,index,list);
    }


    private int count=0;
    public int jump2(int[] nums) {
        if (nums==null||nums.length==0) return 0;
        int start=0;
        int end=nums.length-1;
        helper(nums,start,end);
        return count;
    }
    private void helper(int[] nums,int start ,int end){
        if (end==0) return; //递归终止条件，是更新到end=0，因为是end一直在减
        int index=0;
        for (int i = start; i <end ; i++) {
            if (nums[i]+i>=end){
                index=i;
                count++;
                break;
            }
        }
        helper(nums,start,index);
    }
}
